package cn.bdqn.service.impl;

import cn.bdqn.mapper.SysRightMapper;
import cn.bdqn.pojo.SysRight;
import cn.bdqn.service.SysRightService;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRightServiceImpl implements SysRightService {
    @Resource
    SysRightMapper sysRightMapper;
    @Resource
    SysRightService sysRightService;

    @Override
    public List<SysRight> selectAll() {
        return sysRightMapper.selectList(null);
    }

    @Override
    public List<SysRight> selectRoleId(Long roleId) {
        return sysRightMapper.selectRoleId(roleId);
    }

    @Override
    public Map<String, String> reLoadMap() {
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/css/**", "anon");
        filterMap.put("/fonts/**", "anon");
        filterMap.put("/images/**", "anon");
        filterMap.put("/js/**", "anon");
        filterMap.put("/localcss/**", "anon");
        filterMap.put("/localjs/**", "anon");
        filterMap.put("/dologin", "anon");
        filterMap.put("/logout", "logout");
        //静态演示
//        filterMap.put("/user/list","perms[用户列表]");
//        filterMap.put("/user/add","perms[用户添加]");
////        filterMap.put("/user/edit","perms[用户编辑]");
//        filterMap.put("/user/delete","perms[用户删除]");
//        filterMap.put("/**","authc");
        List<SysRight> lists = sysRightService.selectAll();
        if (lists != null) {
            for (SysRight list : lists) {
                if (list.getRightUrl() != null && !list.getRightUrl().trim().equals("")) {
                    filterMap.put(list.getRightUrl(), "perms[" + list.getRightCode() + "]");
                }
            }
        }
        filterMap.put("/**", "authc");
        return filterMap;
    }


    @Override
    public void reLoadShiro() {
        try{
            HttpServletRequest request=((ServletRequestAttributes)
                    RequestContextHolder.getRequestAttributes()).getRequest();
            ServletContext servletContext=request.getSession().getServletContext();
            AbstractShiroFilter shiroFilter=(AbstractShiroFilter)
                    WebApplicationContextUtils
                            .getRequiredWebApplicationContext(servletContext)
                            .getBean("shiroFilterFactoryBean");
            synchronized (shiroFilter){
                //获取过滤管理器
                PathMatchingFilterChainResolver filterChainResolver=
                        (PathMatchingFilterChainResolver)shiroFilter.getFilterChainResolver();
                DefaultFilterChainManager manager=
                        (DefaultFilterChainManager)filterChainResolver
                                .getFilterChainManager();
                //清空初始配置
                manager.getFilterChains().clear();
                //重新加载动态权限，配置验证规则
                Map<String,String> chains=reLoadMap();
                for (Map.Entry<String,String> entry:chains.entrySet()) {
                    String url=entry.getKey();
                    String chainDefinition=entry.getValue().trim().replace(" ","");
                    manager.createChain(url,chainDefinition);
                }
                System.out.println("更新权限成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
